package com.thanh.icook;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class DetailDishActivity extends Activity{
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_detaildish);
        try {
			getCateDish();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    private void getCateDish() throws JSONException{   	
    	Creadfile read = new Creadfile();
    	String iddish = ListDishActivity.iddish; 
        String _jsonDish = read.readJsonURL("http://icook.dinhchithanh.com/getdishbyname.php?id="+iddish);
		JSONArray entries = new JSONArray(_jsonDish);	
		TextView title = (TextView)findViewById(R.id.detaildish_title);
		TextView description = (TextView)findViewById(R.id.detaildish_description);
		ImageView image = (ImageView)findViewById(R.id.detaildish_image);
		TextView _resource = (TextView)findViewById(R.id.detaildish_resource);
		TextView _howmake = (TextView)findViewById(R.id.detaildish_howmake);
		for(int i=0;i<entries.length();i++){
			JSONObject post = entries.getJSONObject(i);
			title.setText(post.getString("title"));
			description.setText(post.getString("description"));
			image.setImageBitmap(read.DownloadImage(post.getString("urlimage")));
		}	
		String res_term="";
		String _strdishres = read.readJsonURL("http://icook.dinhchithanh.com/getresource.php?id="+iddish);
		JSONArray jsresourcedish = new JSONArray(_strdishres);
		for (int j = 0; j < jsresourcedish.length(); j++) {
			JSONObject dish_res = jsresourcedish.getJSONObject(j);
			res_term += "    + " + dish_res.getString("name")+"\n";
		}
		_resource.setText(res_term);
		String how_term="";
		String _strdishhow = read.readJsonURL("http://icook.dinhchithanh.com/gethowmake.php?id="+iddish);
		JSONArray jshowdish = new JSONArray(_strdishhow);
		for (int k = 0; k < jshowdish.length(); k++) {
			JSONObject dish_how = jshowdish.getJSONObject(k);
			how_term += "    + " + dish_how.getString("name") + "\n"; 
		}
		_howmake.setText(how_term);
    }  
}
